import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import layout from '@/layout/index.vue'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard' }
    }]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/business',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'Business',
        component: () => import('@/views/business'),
        meta: { title: '商家信息' }
      }
    ]
  },
  {
    path: '/account-list',
    component: layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/accountList/index'),
        name: 'AccountList',
        meta: { title: '账号列表' }
      }
    ]
  },
  {
    path: '/banner-area',
    component: layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/bannerArea/index'),
        name: 'BannerArea',
        meta: { title: 'banner区' }
      }
    ]
  },
  {
    path: '/reservation-center',
    component: layout,
    redirect: '/reservation-center/reservation-time',
    name: 'ReservationCenter',
    meta: { title: '预约中心' },
    children: [
      {
        path: 'index',
        component: () => import('@/views/reservationCenter/reservationTime/index'),
        name: 'ReservationTime',
        meta: { title: '预约时间' }
      },
      {
        path: '/membership-card-management',
        component: () => import('@/views/reservationCenter/membershipCardManagement/index.vue'),
        name: 'membership-card-management',
        meta: { title: '会员卡管理' },
        children: [
          {
            path: '/membership-card-management/index',
            component: () => import('@/views/reservationCenter/membershipCardManagement/membershipCardList'),
            name: 'MembershipCardList',
            meta: { title: '会员卡列表' }
          },
          {
            path: '/membershipCardOrderList',
            component: () => import('@/views/reservationCenter/membershipCardManagement/membershipCardOrderList'),
            name: 'MembershipCardOrderList',
            meta: { title: '会员卡订单列表' }
          }
        ]
      },
      {
        path: 'reservation-management',
        component: layout,
        name: 'ReservationManagement',
        meta: { title: '预约管理' },
        children: [
          {
            path: 'service-reservation-management',
            component: () => import('@/views/reservationCenter/reservationManagement/serviceReservationManagement'),
            name: 'ServiceReservationManagement',
            meta: { title: '服务预约管理' },
            children: [
              {
                path: 'service-goods-setting',
                component: () => import('@/views/reservationCenter/reservationManagement/serviceReservationManagement/serviceGoodsSetting'),
                name: 'ServiceGoodsSetting',
                meta: { title: '服务商品设置' }
              },
              {
                path: 'service-reservation-order-list',
                component: () => import('@/views/reservationCenter/reservationManagement/serviceReservationManagement/serviceReservationOrderList'),
                name: 'ServiceReservationOrderList',
                meta: { title: '服务预约订单列表' }
              }
            ]
          },
          {
            path: 'time-reservation-order-management',
            component: () => import('@/views/reservationCenter/reservationManagement/timeReservationOrderManagement'),
            name: 'TimeReservationOrderManagement',
            meta: { title: '时间预约订单管理' }
          },
          {
            path: 'check-in-verification-order-list',
            component: () => import('@/views/reservationCenter/reservationManagement/checkInVerificationOrderList'),
            name: 'CheckInVerificationOrderList',
            meta: { title: '到店核销订单列表' }
          }
        ]
      }
    ]
  }, {
    path: '/product-management',
    component: layout,
    name: 'ProductManagement',
    meta: { title: '商品管理' },
    children: [
      {
        path: '/product-category',
        component: () => import('@/views/productManagement/productCategory'),
        name: 'ProductCategory',
        meta: { title: '商品分类' }
      },
      {
        path: '/product-list',
        component: () => import('@/views/productManagement/productList'),
        name: 'ProductList',
        meta: { title: '商品列表' }
      }
    ]
  },
  {
    path: '/group-buying',
    component: layout,
    name: 'GroupBuying',
    meta: { title: '团购商品' },
    children: [
      {
        path: '/group-buying-category',
        component: () => import('@/views/groupBuying/groupBuyingCategory'),
        name: 'GroupBuyingCategory',
        meta: { title: '商品分类' }
      },
      {
        path: '/group-buying-list',
        component: () => import('@/views/groupBuying/groupBuyingList'),
        name: 'GroupBuyingList',
        meta: { title: '商品列表' }
      }
    ]
  },
  {
    path: '/order-list',
    component: layout,
    children: [{
      path: 'order-list',
      component: () => import('@/views/orderList/index'),
      name: 'OrderList',
      meta: { title: '订单列表' }
    }]
  },
  {
    path: '/group-buying-order-list',
    component: layout,
    children: [{
      path: 'group-buying-order-list',
      component: () => import('@/views/groupBuyingOrderList'),
      name: 'GroupBuyingOrderList',
      meta: { title: '团购订单列表' }
    }]
  },
  {
    path: '/statistics',
    component: layout,
    children: [{
      path: 'statistics',
      component: () => import('@/views/statistics'),
      name: 'Statistics',
      meta: { title: '统计' }
    }]
  },
  {
    path: '/user-complaints-feedback',
    component: layout,
    children: [{
      path: 'user-complaints-feedback',
      component: () => import('@/views/userComplaintsFeedback'),
      name: 'UserComplaintsFeedback',
      meta: { title: '用户投诉反馈' }
    }]
  },
  {
    path: '/mini-program-management',
    component: layout,
    children: [{
      path: 'mini-program-management',
      component: () => import('@/views/miniProgramManagement'),
      name: 'MiniProgramManagement',
      meta: { title: '小程序管理' }
    }]
  },
  {
    path: '/sms-package',
    component: layout,
    name: 'SmsPackage',
    meta: { title: '短信包' },
    children: [
      {
        path: 'sms-package-list',
        component: () => import('@/views/smsPackage/smsPackageList'),
        name: 'SmsPackageList',
        meta: { title: '短信包列表' }
      },
      {
        path: 'purchase-history',
        component: () => import('@/views/smsPackage/purchaseHistory'),
        name: 'PurchaseHistory',
        meta: { title: '购买记录' }
      }
    ]
  },
  {
    path: '/customer-service-faq',
    component: layout,
    name: 'CustomerServiceFaq',
    meta: { title: '客服及常见问题' },
    children: [
      {
        path: 'customer-service-hotline',
        component: () => import('@/views/customerServiceFaq/customerServiceHotline'),
        name: 'CustomerServiceHotline',
        meta: { title: '客服热线' }
      },
      {
        path: 'faq',
        component: () => import('@/views/customerServiceFaq/faq'),
        name: 'Faq',
        meta: { title: '常见问题' }
      }
    ]
  },
  {
    path: '/other-settings',
    component: layout,
    name: 'OtherSettings',
    meta: { title: '其他设置' },
    children: [
      {
        path: 'settings',
        component: () => import('@/views/otherSettings/settings'),
        name: 'Settings',
        meta: { title: '其他设置' }
      },
      {
        path: 'change-password',
        component: () => import('@/views/otherSettings/changePassword'),
        name: 'ChangePassword',
        meta: { title: '修改密码' }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
